#!/usr/bin/env python
# -*- coding: utf-8 -*-

# People Presence - Predesys service plugin
# Version: 0.1.2
# Author: Jose A. Jimenez <jajimc@gmail.com>"
# License: GPL 3.0 (see "license" file)

import sys
sys.dont_write_bytecode = True
import time
import predesys

### SERVICE TEMPLATE ###
def get_service_id():
    return "peoplepresence"
    
def get_version():
    return "0.1.2"
    
def get_service_name():
    return "People Presence"
    
def get_author():
    return "Jose Antonio Jimenez Carmona <jajimc@gmail.com>"
    
def run(args):
    people = []
    
    try:        
        now = time.time()
        names = []
        
        for addressData in predesys.get_log_registered_addresses(15):
            logDate = addressData[2]
            if (now - logDate) < 60:
                address = addressData[0]
                for userId in predesys.get_address_users(address):
                    userName = predesys.get_user_data(userId).get_name()
                    names.append(userName)
                    if names.count(userName) == 1:
                        stationUserId = addressData[1]
                        stationName = predesys.get_user_data(stationUserId).get_name()
                        people.append([userName, stationName, logDate])
    except:
        # ToDo: log errors
        pass
            
    return people
